﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GmInputBox.h"
#include "GbmpNew.h"
#include <QTimer>
#include "GmStyleSheetHelper.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

// GmInputBox 类实现
GmInputBox::GmInputBox(QWidget *parent, const std::wstring& defaultText/* = L""*/)
    : QDialog(parent, Qt::FramelessWindowHint)
{
    ui.setupUi(this);
    std::wstring styleSheetStr = GmStyleSheetHelper::Instance()->GetCurrentStyleData();
    setStyleSheet(QString::fromStdWString(styleSheetStr));
    ui.gbmp_title->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    ui.gbmp_title->setIconVisible(false);
    ui.gbmp_title->setMaxButtonVisible(false);
    ui.gbmp_title->setMinButtonVisible(false);
    setProperty("use_gbmp_title", true);
    connect(ui.gbmp_title, &GmTitleBar::closeRequested, this, &GmInputBox::reject);
    connect(ui.gbmp_title, &GmTitleBar::moveRequested, this, &GmInputBox::onMoveRequested);
    QObject::connect(ui.pushButtonOK, SIGNAL(clicked()), this, SLOT(OnOKButtonClick()));
    QObject::connect(ui.pushButtonCancel, SIGNAL(clicked()), this, SLOT(OnCancelButtonClick()));
    ui.lineEditInput->setPlaceholderText(QString::fromStdWString(defaultText));
}

GmInputBox::~GmInputBox()
{

}

void GmInputBox::SetTitle(const std::wstring& title)
{
    this->setWindowTitle(QString::fromStdWString(title));
    ui.gbmp_title->setText(QString::fromStdWString(title));
}

void GmInputBox::SetMessage(const std::wstring& message)
{
    ui.labelMessage->setText(QString::fromStdWString(message));
}

void GmInputBox::SetInputText(const std::wstring& inputText)
{
    ui.lineEditInput->setText(QString::fromStdWString(inputText));
}

void GmInputBox::SetAutoCloseAfterMiliSeconds(int miliSeconds)
{
    if(miliSeconds > 0)
    {
        m_upIdleTimer = NEW_AS_OWNER_PTR(QTimer, this);
        connect(m_upIdleTimer.get(), SIGNAL(timeout()), SLOT(OnIdle()) );
        m_upIdleTimer->start(miliSeconds);
    }
}

void GmInputBox::OnOKButtonClick()
{
    QDialog::accept();
}

void GmInputBox::OnCancelButtonClick()
{
    QDialog::reject();
}

void GmInputBox::OnIdle()
{
    QDialog::accept();
}

std::wstring GmInputBox::GetInputText()
{
    return ui.lineEditInput->text().toStdWString();
}

void GmInputBox::onMoveRequested(const QPoint& cursorPos, const QPoint& offset)
{
    Q_UNUSED(cursorPos);
    move(pos() + offset);
}
